import Vue from 'vue'
import Router from 'vue-router'
import Layout from '@views/layout/index.vue'

const originalPush = Router.prototype.push
Router.prototype.push = function push (location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}
Vue.use(Router)
const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@views/login/index.vue'),
    hidden: true
  },
  {
    path: '/404',
    name: 'Page404',
    component: () => import('@views/error-page/404.vue'),
    hidden: true
  },
  { path: '*', name: 'Page404', redirect: '/404', hidden: true },
  {
    path: '/',
    component: Layout,
    redirect: '/home',
    hidden: true,
    children: [
      {
        path: '/home',
        name: 'Home',
        component: () => import('@views/home'),
        meta: {
          title: '首页',
          icon: 'home_icon'
        }
      }
    ]
  }
]

export const createRouter = () => new Router({
  mode: 'history', // require service support
  // base: '/ies/',
  scrollBehavior: () => ({
    y: 0
  }),
  // base: process.env.VUE_APP_BASE_URL,
  routes
})
const router = createRouter()

export function resetRouter () {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
